package cn.com.cetccst.activation.ui.dialog.viewmodel;


import static cn.com.cetccst.tools.kv.CommonKV.kvStore;

import android.text.TextUtils;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModel;

import cn.com.cetccst.activation.kv.KVKeys;
import cn.com.cetccst.activation.network.ActivationHttpRequester;
import cn.com.cetccst.activation.network.response.AuthTypeResponse;
import cn.com.cetccst.activation.network.response.AuthResponse;
import cn.com.cetccst.activation.ui.viewmodel.ActivationViewModel;
import cn.com.cetccst.tools.log.CommonLogger;
import rx.Observable;
import rx.functions.Func1;

public class ProcessViewModel extends ViewModel {
    public ObservableField<String> prompt = new ObservableField<>();
    public ObservableBoolean confirm = new ObservableBoolean(false);

    public interface Callback {
        void onStart();

        void onSuccess();
    }

    public void requestAuth(FragmentActivity activity, ActivationViewModel.AuthData authData, Callback callback) {

        confirm.set(false);
        if (!TextUtils.isEmpty(authData.getOtaServer())) {
            prompt.set("一体化安全保密套件激活中");
            callback.onStart();
            ActivationHttpRequester.initialize(activity, authData.getOtaServer());
            ActivationHttpRequester.getInstance().requestOTA()
                    .flatMap(otaResponse -> {
                        //处理第一次请求的响应
                        if (otaResponse != null) {
                            return ActivationHttpRequester.getInstance().requestActivation(activity); //第二次请求
                        }
                        //TODO 后续自定义一个异常类型，这里不弄了
                        throw new RuntimeException("网络异常1");
                    })
                    .flatMap((Func1<AuthTypeResponse, Observable<AuthResponse>>) authTypeResponse -> {
                        if (authTypeResponse != null) {
                            return ActivationHttpRequester.getInstance().requestAuth(authData, authTypeResponse.getActivationToken()); //第三次请求
                        }
                        throw new RuntimeException("网络异常2");
                    })
                    .subscribe(new rx.Observer<>() {
                        @Override
                        public void onCompleted() {
                            if (!TextUtils.isEmpty(kvStore.decodeString(KVKeys.KEY_ACCESS_TOKEN))) {
                                callback.onSuccess();
                                CommonLogger.info("激活完成！");
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            prompt.set("激活失败！");
                            confirm.set(true);
                            CommonLogger.error("激活失败", e);
                        }

                        @Override
                        public void onNext(AuthResponse response) {
                            kvStore.encode(KVKeys.KEY_ACCESS_TOKEN, response.getAccessToken());
                            if (response.getErrCode() != 0) {
                                prompt.set("激活失败！");
                                confirm.set(true);
                                CommonLogger.info("激活失败");
                            }
                        }
                    });
        }

    }

}
